package com.pp.common.utils.logger;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pp.common.exception.BizException;
import org.slf4j.Logger;

import java.lang.reflect.Method;


public class LoggerUtil {
    private static final String LOG_FORMAT = "\n{}\n  request {}\n  result {}";

    public static void printLog(Logger log, Class clazz, Method method, String jsonArgs, Object returnVal, Throwable e) {
        String methodName = clazz.getName() + "." + method.getName();
        if (e == null || (e instanceof BizException && e.getCause() == null)) {
            log.info(LOG_FORMAT, methodName, jsonArgs, JSONObject.toJSONString(returnVal));
        } else {
            log.error(LOG_FORMAT, methodName, jsonArgs, JSONObject.toJSONString(returnVal), e);
        }
    }

    public static void printLog(Logger log, String clazz, String method, Object[] args, Object returnVal, Throwable e) {
        Object arg = null;
        if (args != null && args.length > 0) {
            arg = args[0];
        }
        String methodName = clazz + "." + method;
        if (e == null || e instanceof BizException) {
            log.info(LOG_FORMAT, methodName, JSONObject.toJSONString(arg), JSONObject.toJSONString(returnVal));
        } else {
            log.error(LOG_FORMAT, methodName, JSONObject.toJSONString(arg), JSONObject.toJSONString(returnVal), e);
        }
    }

    public static void printLog(Logger log, String clazz, String method,Object jsonArgs, Object returnVal,Throwable e) {
        String methodName = clazz + "." + method;
        if (e == null || e instanceof BizException) {
            log.info(LOG_FORMAT, methodName, JSON.toJSONString(jsonArgs,true), JSON.toJSONString(returnVal,true));
        } else {
            log.error(LOG_FORMAT, methodName, JSON.toJSONString(jsonArgs,true), JSON.toJSONString(returnVal,true), e);
        }
    }

}